<!--- Document Information -----------------------------------------------------

Title:      HibernateFactory.cfc

Author:     Mark Mandel
Email:      mark@compoundtheory.com

Website:    http://www.compoundtheory.com

Purpose:    Factory for getting to Hibernate objects

Usage:      

Modification Log:

Name			Date			Description
================================================================================
Mark Mandel		27/06/2006		Created

------------------------------------------------------------------------------->

<cfcomponent name="HibernateFactory" hint="Factory for getting to Hibernate objects">

<cfscript>
	instance = StructNew();
</cfscript>

<!------------------------------------------- PUBLIC ------------------------------------------->

<cffunction name="init" hint="Constructor: Loads the hibernate.cfg.xml from here, and any *.hbm.xml files that are in this directory" access="public" returntype="HibernateFactory" output="false">
	<cfargument name="configPath" hint="Directory that the hibernate.cfg.xml is in" type="string" required="Yes">
	<cfscript>
		setConfigPath(arguments.configPath);
		
		initHibernateLibrary();
		
		return this;
	</cfscript>
</cffunction>

<cffunction name="configure" hint="Runs the Hibernate configure method" access="public" returntype="any" output="false">
	<cfscript>
		//create the config
		var config = createObject("java", "java.io.File").init(getConfigPath() & "/hibernate.cfg.xml");
		var configDir = createObject("java", "java.io.File").init(getConfigPath());
		var configuration = create("org.hibernate.cfg.Configuration").configure(config);
				
		//by default, read all the .hmb.xml mappings in the config dir
		configuration.addDirectory(configDir);
		
		return configuration;
	</cfscript>
</cffunction>

<cffunction name="create" hint="Creates a hibernate object" access="public" returntype="any" output="false">
	<cfargument name="className" hint="The class name of the hiberanate class to load" type="string" required="Yes">
	<cfreturn getJavaLoader().create(arguments.className)>
</cffunction>

<cffunction name="getHibernateVersion" hint="The hibernate version in use" access="public" returntype="string" output="false">
	<cfreturn "3.1.1">
</cffunction>

<!------------------------------------------- PACKAGE ------------------------------------------->

<!------------------------------------------- PRIVATE ------------------------------------------->

<cffunction name="initHibernateLibrary" hint="Inititalises all the java libs to be loaded in" access="public" returntype="void" output="false">
	<cfscript>
		var root = GetDirectoryFromPath(getMetaData(this).path);
		var library = ArrayNew(1);
		var hibernateRoot = root & "hibernate" & getHibernateVersion() & "/";
		var jdbcRoot = root & "jdbc/";
		
		//custom / generated classes here
		ArrayAppend(library, getConfigPath());
		library = loadDirectory(getConfigPath(), library);
		
		//jdbc
		library = loadDirectory(jdbcRoot, library);
		
		//hibernate
		library = loadDirectory(hibernateRoot, library);
		
		setJavaLoader(createObject("component", "JavaLoader").init(library));
	</cfscript>
</cffunction>

<cffunction name="loadDirectory" hint="Loads the JAR file paths into the array" access="public" returntype="array" output="false">
	<cfargument name="path" hint="The path to oad" type="string" required="Yes">
	<cfargument name="library" hint="The array of classpaths" type="array" required="Yes">
	<cfscript>
		var qJars = 0;
	</cfscript>
	<cfdirectory action="list" directory="#arguments.path#" filter="*.jar" name="qJars">
	
	<cfloop query="qJars">
		<cfscript>
			ArrayAppend(arguments.library, arguments.path & name);
		</cfscript>
	</cfloop>
	
	<cfscript>
		return arguments.library;
	</cfscript>
</cffunction>

<cffunction name="getConfigPath" access="private" returntype="string" output="false">
	<cfreturn variables.ConfigPath />
</cffunction>

<cffunction name="setConfigPath" access="private" returntype="void" output="false">
	<cfargument name="ConfigPath" type="string" required="true">
	<cfset variables.ConfigPath = arguments.ConfigPath />
</cffunction>

<cffunction name="getJavaLoader" access="private" returntype="JavaLoader" output="false">
	<cfreturn variables.JavaLoader />
</cffunction>

<cffunction name="setJavaLoader" access="private" returntype="void" output="false">
	<cfargument name="JavaLoader" type="JavaLoader" required="true">
	<cfset variables.JavaLoader = arguments.JavaLoader />
</cffunction>

</cfcomponent>